<template>
		<view>
			<view class="content">
				收货人：<input v-model="name" placeholder="收货人姓名" maxlength="20"></input>
				手机号：<input v-model="phone" type="number" maxlength="11" placeholder="收货人手机号"></input>
				<view ></view>
				<view @click="btnChooseLocation">
					<view>{{address}}</view>
					<view></view>
				</view>
			</view>
			<view class="savebtn" @click="btnSaveAddress">保存</view>
		</view>
</template>

<script>
	import { mapMutations } from 'vuex'
	import cloudApi from "@/common/cloundApi.js"
	export default {
		data() {
			return {
				_id:null,
				name:"",
				phone:"",
				address:"选取自提点",
				longitude:"",
				latitude:""
			}
		},
		onLoad(options) {
			if(options.item){
				this._id = options.item;
				cloudApi.call({
					name:"address",
					data:{
						action:"get",
						_id:this._id
					},
					success:(res)=>{
						this.name = res.result[0].name;
						this.phone = res.result[0].phone;
						this.address = res.result[0].address;
						this.longitude = res.result[0].geopoint.coordinates[0];
						this.latitude = res.result[0].geopoint.coordinates[1];
					}
				})
			}
		},
		methods: {
		...mapMutations('m_user', ['deleteAddress']),
			btnChooseLocation(){
				uni.getLocation({
					success: (res) => {
						uni.chooseLocation({
							latitude:res.latitude,
							longitude:res.longitude,
							success: (res) => {
								this.address= res.address+res.name;
								this.longitude=res.longitude;
								this.latitude=res.latitude;
							}
						})
					}
				})
			},
			btnSaveAddress(){
			this.storageAddress = JSON.parse(uni.getStorageSync('address'))
				cloudApi.call({
					name:"address",
					data:{
						action:this._id?"update":"create",
						_id:this._id,
						name:this.name,
						phone:this.phone,
						address:this.address,
						longitude:this.longitude,
						latitude:this.latitude
					},
					success: (res) => {
						if(this.storageAddress._id===this._id){
							this.deleteAddress()
						}
						uni.navigateBack({})						
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
.savebtn{
	width: 90%;
	height: 24px;
	padding: 5px;
	border-radius: 4px;
	color: #fff;
	background: #c00000;
	text-align: center;
	margin: 0 auto;
}
.content{
	padding: 15px;
	input{
		font-size: 14px;
		border-bottom: 1px solid #ededed;
		margin: 5px 0;
	}
}
</style>
